﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Collections;

namespace TerenXNA
{
    public class Tile
    {
        // wymiary pola w metrach + 1, nalezy jednak pamietac
        // zeby zawsze uwzglednic skale swiata metrow/unit
        public static int width = 1025;
        public static int height = 1025;
        
        // wspolrzedne pola x,y
        private int wspx;
        private int wspy;

        // czy wczytane
        public bool loaded = false;

        // elementy pola 
        public Terrain terrain;
        public Water water;
        //public WorldObject[] worldObjects;
        public ArrayList worldObjects;
        //( tu dodawac kolejne )

        // utworzenie nowego pola
        public Tile( int x, int y)
        {
            this.wspx = x;
            this.wspy = y;
        }

        // wczytanie pola
        public void load()
        {
            terrain = new Terrain(wspx, wspy);
            worldObjects = null;
            //water = new Water();
            //water.load();

            loaded = true;
        }
        
        // render pola
        public void draw(int lod, Matrix viewMatrix, Matrix projectionMatrix, Matrix translateMatrix) 
        {
            if (!loaded) return;

            //render terenu
            if (World.tileTerrainDraw)
                terrain.draw(lod);
            
            //render wody
            //water.Draw(lod);

            //render obiektow
            if (World.tileObjectDraw && worldObjects != null)
            {
                foreach (WorldObject worldObject in worldObjects)
                {
                    Matrix[] transforms = new Matrix[worldObject.model.Bones.Count];
                    worldObject.model.CopyAbsoluteBoneTransformsTo(transforms);

                    foreach (ModelMesh mesh in worldObject.model.Meshes)
                    {
                        foreach (BasicEffect effect in mesh.Effects)
                        {
                            effect.EnableDefaultLighting();

                            effect.View = viewMatrix;
                            effect.Projection = projectionMatrix;
                            effect.World = transforms[mesh.ParentBone.Index] *
                                Matrix.CreateScale(0.1f) *
                                translateMatrix *
                                Matrix.CreateTranslation(worldObject.position.Y, 0, worldObject.position.X);
                        }
                        mesh.Draw();
                    }
                }
            }

            //( tu dodawac kolejne )

        }

    }
}
